package xyz.dava.sync.fm.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.new")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "oldDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.old")
    public DataSource oldDataSource() {
        return new DruidDataSource();
    }

    @Bean
    @Primary
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public JdbcTemplate oldJdbcTemplate(@Qualifier("oldDataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
}
